package com.example.demo.practice;

import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.content.Intent;
import android.support.v4.app.FragmentActivity; // 注意这里我们导入的V4的包，不要导成app的包了
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 主页面内容
 * Created by dm on 16-1-19.
 */
public class Home extends FragmentActivity implements View.OnClickListener{
    // 初始化顶部栏显示
    private ImageView titleLeftImv;
    private TextView titleTv;
    // 定义4个Fragment对象
    private Table1 fg1;
    private Table2 fg2;
    private Table3 fg3;
    private Table4 fg4;
    private Table5 fg5;
    // 帧布局对象，用来存放Fragment对象
    private FrameLayout frameLayout;
    private String user = "";
    // 定义每个选项中的相关控件
    private RelativeLayout firstLayout;
    private RelativeLayout secondLayout;
    private RelativeLayout thirdLayout;
    private RelativeLayout fourthLayout;
    private RelativeLayout fifthLayout;
    private ImageView firstImage;
    private ImageView secondImage;
    private ImageView thirdImage;
    private ImageView fourthImage;
    private ImageView fifthImage;
    private TextView firstText;
    private TextView secondText;
    private TextView thirdText;
    private TextView fourthText;
    private TextView fifthText;
    // 定义FragmentManager对象管理器
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //取消状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        AppManager.addActivity(this);//加入堆栈
        initView(); // 初始化界面控件
        fragmentManager = getSupportFragmentManager();
        setChoiceItem(0); // 初始化页面加载时显示第一个选项卡
    }

    /**
     * 初始化页面
     */
    private void initView() {
        // 初始化页面标题栏
        titleTv = findViewById(R.id.title_text_tv);
        titleTv.setText(getString(R.string.app_name));
        // 初始化底部导航栏的控件
        firstImage = findViewById(R.id.first_image);
        secondImage = findViewById(R.id.second_image);
        thirdImage = findViewById(R.id.third_image);
        fourthImage = findViewById(R.id.fourth_image);
        fifthImage = findViewById(R.id.fifth_image);

        firstText = findViewById(R.id.first_text);
        secondText = findViewById(R.id.second_text);
        thirdText = findViewById(R.id.third_text);
        fourthText = findViewById(R.id.fourth_text);
        fifthText = findViewById(R.id.fifth_text);

        firstLayout = findViewById(R.id.first_layout);
        secondLayout = findViewById(R.id.second_layout);
        thirdLayout = findViewById(R.id.third_layout);
        fourthLayout = findViewById(R.id.fourth_layout);
        fifthLayout = findViewById(R.id.fifth_layout);

        firstLayout.setOnClickListener(Home.this);
        secondLayout.setOnClickListener(Home.this);
        thirdLayout.setOnClickListener(Home.this);
        fourthLayout.setOnClickListener(Home.this);
        fifthLayout.setOnClickListener(Home.this);
        //点击用户图标进入用户界面
        ImageView imageView = findViewById(R.id.setting);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Home.this, SettingActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.first_layout:
                setChoiceItem(0);
                break;
            case R.id.second_layout:
                setChoiceItem(1);
                break;
            case R.id.third_layout:
                setChoiceItem(2);
                break;
            case R.id.fourth_layout:
                setChoiceItem(3);
                break;
            case R.id.fifth_layout:
                setChoiceItem(4);
                break;
            default:
                break;
        }
    }

    /**
     * 设置点击选项卡的事件处理
     *
     * @param index 选项卡的标号：0, 1, 2, 3
     */
    private void setChoiceItem(int index) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        clearChoice(); // 清空, 重置选项, 隐藏所有Fragment
        hideFragments(fragmentTransaction);

        switch (index) {
            case 0:
                firstImage.setImageResource(R.drawable.tab_survey_y);

                // 如果fg1为空，则创建一个并添加到界面上
                if (fg1 == null) {
                    fg1 = new Table1();
                    fragmentTransaction.add(R.id.content, fg1);
                } else {
                    // 如果不为空，则直接将它显示出来
                    fragmentTransaction.show(fg1);
                }
                break;
            case 1:
                secondImage.setImageResource(R.drawable.tab_monitor_y);
                if (fg2 == null) {
                    fg2 = new Table2();
                    fragmentTransaction.add(R.id.content, fg2);
                } else {
                    fragmentTransaction.show(fg2);
                }
                break;
            case 2:
                thirdImage.setImageResource(R.drawable.tab_area_y);
                if (fg3 == null) {
                    fg3 = new Table3();
                    fragmentTransaction.add(R.id.content, fg3);
                } else {
                    fragmentTransaction.show(fg3);
                }
                break;
            case 3:
                fourthImage.setImageResource(R.drawable.tab_enterprise_y);
                if (fg4 == null) {
                    fg4 = new Table4();
                    fragmentTransaction.add(R.id.content, fg4);
                } else {
                    fragmentTransaction.show(fg4);
                }
                break;

            case 4:
                fifthImage.setImageResource(R.drawable.tab_device_y);
                if (fg5 == null) {
                    fg5 = new Table5();
                    fragmentTransaction.add(R.id.content, fg5);
                } else {
                    fragmentTransaction.show(fg5);
                }
                break;
        }
        fragmentTransaction.commit(); // 提交
    }

    /**
     * 当选中其中一个选项卡时，其他选项卡重置为默认
     */
    private void clearChoice() {

        firstImage.setImageResource(R.drawable.tab_survey_n);

        secondImage.setImageResource(R.drawable.tab_monitor_n);


        thirdImage.setImageResource(R.drawable.tab_area_n);


        fourthImage.setImageResource(R.drawable.tab_enterprise_n);


        fifthImage.setImageResource(R.drawable.tab_device_n);

    }

    /**
     * 隐藏Fragment
     */
    private void hideFragments(FragmentTransaction fragmentTransaction) {
        if (fg1 != null) {
            fragmentTransaction.hide(fg1);
        }
        if (fg2 != null) {
            fragmentTransaction.hide(fg2);
        }
        if (fg3 != null) {
            fragmentTransaction.hide(fg3);
        }
        if (fg4 != null) {
            fragmentTransaction.hide(fg4);
        }
        if (fg5 != null) {
            fragmentTransaction.hide(fg5);
        }
    }

    /**
     * 覆写返回键事件
     *
     * @param keyCode {int}按键id
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                new AlertDialog.Builder(Home.this)
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setTitle(R.string.dialog_prompt)
                        .setMessage(R.string.dialog_exit)
                        .setNegativeButton(R.string.dialog_cancel, null)
                        .setPositiveButton(R.string.dialog_sure, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                finish();
                                System.exit(0);
                            }
                        })
                        .show();
                break;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);
    }
}